package com.wing.ext.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wing.common.Constants;
import com.wing.common.util.PageBean;
import com.wing.common.util.WordFilter;
import com.wing.ext.domain.Information;
import com.wing.ext.domain.Site_user;
import com.wing.ext.service.InformationManager;
import com.wing.ext.service.Site_userManager;
import com.wing.sys.domain.Code;
import com.wing.sys.service.CodeManager;

/**
 * 
 * 
 * @author please  write your big name
 * 
 */
 
@Controller
@RequestMapping(value = "/ext/information")
public class InformationController {

	@Autowired
	private CodeManager codeManager;
	@Autowired
	private InformationManager informationManager;
	@Autowired
	private Site_userManager site_userManager;
	
	/**
	 * 获得列表数据
	 *  
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String list() {
		return "/ext/information/informationIndex";
	}
	
	@RequestMapping(value = "get/{url}", method = RequestMethod.GET)
	public String delete(@PathVariable String url) {
		return "/ext/information/"+url;
	}
		
	/**
	 * 是根据信息pid获取的信息,生成树
	 * 
	 * @param request
	 * @param response
	 * @param id
	 * @return
	 * 
	 * @throws ServletException,
	 *             IOException
	 */
	@RequestMapping(value = "showTree", method = RequestMethod.GET)
	public void showTree(HttpServletRequest request,
			HttpServletResponse response) {
		String siteId = (String) request.getSession().getAttribute(
				Constants.SESSION_SITE_ID);
		response.setContentType("text/xml;charset=UTF-8");
		response.setHeader("Pragma", "No-cache");// HTTP 1.1
		response.setHeader("Cache-Control", "no-cache");// HTTP 1.0
		response.setHeader("Expires", "0");// 防止被proxy

		String pid = request.getParameter("id");
		boolean firstFlag = false;
		if (pid == null || pid.equalsIgnoreCase("")) {
			pid = Information.INFORMATIONROOT;// 我数据库中一级节点的parentid都是-1
			firstFlag = true;
		}
		try {
			// 根据上一级节点,查询该节点下的所有节点
			// String aa = codeManager.createDHTMLXML(pid,firstFlag,domainId);
			response.getWriter().write(
					informationManager.createDHTMLXML(pid, firstFlag, siteId));
			response.getWriter().close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	@RequestMapping(value = "list", method = RequestMethod.POST)
	public @ResponseBody PageBean list(String codeId,int currentPage, String pageSize,
			HttpServletRequest request) {
		String type_id = null;
		if(codeId!=null && !codeId.equals("") && !codeId.equals("null")){
			Code c = codeManager.findById(codeId);
			type_id=c==null?null:c.getCode();
		}
		String siteId = (String) request.getSession().getAttribute(
				Constants.SESSION_SITE_ID);
		int _iPageSize = 0;
		if (pageSize == null) {
			_iPageSize = PageBean.DEFAULT_PAGESIZE;
		} else {
			_iPageSize= Integer.parseInt(pageSize);
		}
		return  informationManager.findInformationsByType(currentPage,_iPageSize,siteId,type_id,null);
	}
		
	@RequestMapping(value = "getLists", method = RequestMethod.POST)
	public @ResponseBody PageBean getLists(String toHome,String site_id, String type_id,
			int currentPage, String pageSize,
			HttpServletRequest request) {
		int _iPageSize = 0;
		if (pageSize == null) {
			_iPageSize = PageBean.DEFAULT_PAGESIZE;
		} else {
			_iPageSize= Integer.parseInt(pageSize);
		}
		return  informationManager.findInformationsByType(currentPage,_iPageSize,site_id,type_id,toHome);
	}
		
	@RequestMapping(value = "gethLists", method = RequestMethod.POST)
	public @ResponseBody PageBean gethLists(String toHome,String site_id, String type_id,
			Integer currentPage, String pageSize,
			HttpServletRequest request) {
		if(currentPage==null)currentPage=1;
		int _iPageSize = 0;
		if (pageSize == null) {
			_iPageSize = PageBean.DEFAULT_PAGESIZE;
		} else {
			_iPageSize= Integer.parseInt(pageSize);
		}
		return  informationManager.findHInformationsByType(currentPage,_iPageSize,site_id,type_id,toHome);
	}
		
	/**
	 * 获取查询后的索引
	 * @param site_id
	 * @param type_id
	 * @param type_id2
	 * @return
	 */
	@RequestMapping(value = "getScIndex", method = RequestMethod.POST)
	public @ResponseBody List getScIndex(String site_id, String inforField,String type_id) {
		return  informationManager.getSearchIndex(site_id,inforField,type_id);
	}
		
	@RequestMapping(value = "search", method = RequestMethod.POST)
	public @ResponseBody PageBean search(String site_id, String inforField,String type_id,
			Integer currentPage, String pageSize,
			String type_id2) {
		if(currentPage==null)currentPage=1;
		int _iPageSize = 0;
		if (pageSize == null) {
			_iPageSize = PageBean.DEFAULT_PAGESIZE;
		} else {
			_iPageSize= Integer.parseInt(pageSize);
		}
		return  informationManager.findInformationsByTypeField(currentPage,_iPageSize,site_id,inforField,type_id,type_id2);
	}
	
	/**
	 * 打开编辑页面
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "toForm/{id}", method = RequestMethod.GET)
	public String toEditPage(@PathVariable String id, Model model) {
		Information information = null;
		if(!id.equals("0")){
			information = informationManager.findById(id);
		}else{
			information = new Information();
		}
		model.addAttribute("information", information);
		return "/ext/information/informationForm";
	}
	
	
	@RequestMapping(value = "save", method = RequestMethod.POST)
	public  @ResponseBody  String save(Information information, HttpServletRequest request, HttpServletResponse response) {
		String userId = (String) request.getSession().getAttribute(
				Constants.SESSION_SITE_USER_ID);
		informationManager.saveInformation(information,userId);
		return "success";
	}
	
	
	@RequestMapping(value = "editcontent", method = RequestMethod.POST)
	public  @ResponseBody  String editcontent(Information information, String veryCode0, HttpServletRequest request, HttpServletResponse response) {
		String validateC = (String) request.getSession().getAttribute("validateCode");
		String eidt_editor = request.getParameter("eidt_editor");
		if(veryCode0==null||"".equals(veryCode0)){
			return "codenull";
		}else{
			if(!validateC.equals(veryCode0.toUpperCase())){ 
				return "failure";          
			}              
		}   
		List<String> wordList=new ArrayList<String>();   
        wordList.add("TMD");   
        wordList.add("变态");
        wordList.add("法轮");
        if(eidt_editor!=null){
        	information.setContent(WordFilter.filter(eidt_editor, wordList));
        }
		informationManager.updateInformationContent(information);
		return "success";
	}
	
	@RequestMapping(value = "send", method = RequestMethod.POST)
	public  @ResponseBody  String send(Information information, HttpServletRequest request, HttpServletResponse response) {
		String validateC = (String) request.getSession().getAttribute("validateCode");
		String veryCode = request.getParameter("veryCode");
		if(veryCode==null||"".equals(veryCode)){
			return "codenull";
		}else{
			if(!validateC.equals(veryCode.toUpperCase())){ 
				return "failure";          
			}              
		}   
		List<String> wordList=new ArrayList<String>();   
        wordList.add("TMD");   
        wordList.add("变态");
        
        if(information.getTitle()!=null){
        	information.setTitle(WordFilter.filter(information.getTitle(), wordList));
        }
        if(information.getContent()!=null){
        	information.setContent(WordFilter.filter(information.getContent(), wordList));
        }
		String userId = (String) request.getSession().getAttribute(
				Constants.SESSION_SITE_USER_ID);
		informationManager.saveInformation(information,userId);
		return "success";
	}
	@RequestMapping(value = "find/{id}", method = RequestMethod.POST)
	public  @ResponseBody  Information find(@PathVariable String id) {
		Information information = null;
		if(!id.equals("0")){
			information = informationManager.findById(id);
			Site_user su = site_userManager.findById(information.getCreator());
			information.setSentName(su==null?"":su.getLogin_name());
		}else{
			information = new Information();
		}
		return information;
	}

	
	/**
	 * 打开跟帖页面
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "toReceipt/{id}", method = RequestMethod.GET)
	public String toReceipt(@PathVariable String id, Model model) {
		model.addAttribute("id", id);
		return "/ext/information/xxzs";
	}
	@RequestMapping(value = "getReceipt", method = RequestMethod.POST)
	public @ResponseBody PageBean getReceipt(String site_id, String id,
			int currentPage, String pageSize,
			HttpServletRequest request) {
		int _iPageSize = 0;
		if (pageSize == null) {
			_iPageSize = PageBean.DEFAULT_PAGESIZE;
		} else {
			_iPageSize= Integer.parseInt(pageSize);
		}
		return  informationManager.getReceiptInformationsByPid(currentPage,_iPageSize,site_id,id);
	}
	@RequestMapping(value = "disable/{id}")
	public @ResponseBody String disable(@PathVariable String id,
			HttpServletRequest request) {
		String siteId = (String) request.getSession().getAttribute(
				Constants.SESSION_SITE_ID);
		if(id==null || siteId==null){
			return Constants.DEFAULT_AJAX_FAILURE;
		}
		Information information = informationManager.findById(id);
		if(information==null){
			return Constants.DEFAULT_AJAX_FAILURE;
		}
		if(!information.getSite_id().equals(siteId)){
			return Constants.DEFAULT_AJAX_FAILURE;
		}
		information.setStatus(Information.STATUS_DISABLE);
		informationManager.updateInformation(information);
		return Constants.DEFAULT_AJAX_SUCCESS;
	}
	
	@RequestMapping(value = "delinformations", method = RequestMethod.POST)
	public @ResponseBody String delinformations(String[] chk) throws IOException {
		informationManager.deleteInformations(chk);
		return ""+chk.length;
	}
}

